/**
 * PK10的逻辑处理
 */
initGame(game, function() {
    // 填充tab页
    fill_tabs(['两面', '冠亚和', '1-5名', '6-10名']);

    fill_guanya_row();
    fill_table();
}, true);

// 填充冠亚行
function fill_guanya_row() {
    var types = ["大", "小", "单", "双"];
    for (var i = 0; i < types.length; i++) {
        var t = types[i];
        add_bet_item('冠亚' + t, '冠亚' + t, $(".gyh"));
    }

    var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
    $(".gyh2").append(ul);
    for (var i = 3; i <= 19; i++) {
        add_bet_item("冠亚" + i, i, ul);
        if (i == 17) {
            ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            $(".gyh2").append(ul);
        }
    }
}

// 填充投注区域表格
function fill_table() {
    var ths = ["冠军", "亚军", "第三名", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"];
    var arr = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
    var types = ["大", "小", "单", "双", "龙", "虎"];
    
    var div = $(".ten");
    div.empty();
    for (var j = 0; j < ths.length; j++) {
        add_bet_th(ths[j], div);

        if (j < 5)
            var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
        else
            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');

        add_bet_item(arr[j] + '单', '单', ul);
        add_bet_item(arr[j] + '大', '大', ul);
        if ( j < 5) {
            add_bet_item(arr[j] + '龙', '龙', ul);
        }
        add_bet_item(arr[j] + '双', '双', ul);
        add_bet_item(arr[j] + '小', '小', ul);
        if ( j < 5) {
            add_bet_item(arr[j] + '虎', '虎', ul);
        }

        div.append(ul);
    }

    var arr2 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];

    // 1-5名
    for (var j = 0; j < 5; j++) {
        add_bet_th(ths[j], $(".page3"));
        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        for (var i = 1; i <= 10; i++) {
            add_bet_item(arr[j] + arr2[i - 1], i, ul);
        }
        $(".page3").append(ul);
    }    

    // 6-10名
    for (var j = 5; j < 10; j++) {
        add_bet_th(ths[j], $(".page4"));
        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        for (var i = 1; i <= 10; i++) {
            add_bet_item(arr[j] + arr2[i - 1], i, ul);
        }
        $(".page4").append(ul);
    }
}

window.lastest_result2 = function() {
    var c = Result.last();
    if (!c || !c.red) return [];
    var red = c.red;
    var r = [];

    // 冠亚军和
    var gy = parseInt(red[0]) + parseInt(red[1]);
    r.push(gy);

    if (gy > 11) r.push('大');
    else r.push('小');
    if (gy % 2 == 0) r.push('双');
    else r.push('单');

    // 1~5龙虎
    for (var i = 0; i < 5; i++) {
        var v0 = parseInt(red[i]), v1 = parseInt(red[9 - i]);
        if (v0 > v1) r.push('龙');
        else r.push('虎');
    }
    return r;
}

// 检测能否下注，看下单号1-10是否满足要求
window.customCheckBetList = function(type, issue, data) {
    if (type != 'bjpk10' && type != 'zypk10') return true;

    var limit = Util.getNumberLimit(type);
    if (limit <= 0 || limit >= 10) return true;

    var arr1 = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
    var arr2 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
    var m = {};
    for (var k in data) {
        if (k.length != 2) continue;
        if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
            if (!m[k[0]]) m[k[0]] = {};
            m[k[0]][k[1]] = true;
        }
    }
    if (Util.isEmpty(m)) return true;

    // 获取下已经下注的单据
    var bets = context.bets[type];
    if (bets) bets = bets[issue] || [];
    else bets = [];
    for (var i in bets) {
        var k = bets[i].target;
        if (k.length != 2) continue;
        if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
            if (!m[k[0]]) m[k[0]] = {};
            m[k[0]][k[1]] = true;
        }
    }

    var total = 0;

    for (var k in m) {
        var m2 = m[k];
        if (Object.keys(m2).length > limit) {
            var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
            l = l.replace(/__g__/, get_game_display_name(type));
            l = l.replace(/__v__/g, limit);
            alert2(l, true);
            return false;
        }
    }

    return true;
} 